//
//  ViewController.m
//  homeWorkForQQMV
//
//  Created by 苗钧豪 on 16/8/12.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "list.h"
#import "Play.h"
#import "CollectionViewCell.h"
#import "myCollectionViewCell.h"
#import "Singer.h"
#import "People.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic , strong) NSMutableArray *arr;
@property (nonatomic , strong) NSMutableArray *Arr;
@property (nonatomic , strong) list *listobjc;
@property (nonatomic , strong) Singer *singer;
@property (nonatomic , strong) UICollectionView *collectionView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getup];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"1"];
    [_collectionView registerNib:[UINib nibWithNibName:@"myCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"2"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
        cell.frame = CGRectMake(0, 0, self.view.bounds.size.width, 200);
        if (_arr.count != 0) {
            _listobjc = _arr[indexPath.row];
            _singer = [[Singer alloc]initWithPicurl:_singer.picurl mvtitle:_singer.mvtitle mvdesc:_singer.mvdesc singer_name:_singer.singer_name];            NSURL *url = [NSURL URLWithString:_singer.picurl];
            _singer = _listobjc.mvlist[indexPath.row];
            NSData *data = [NSData dataWithContentsOfURL:url];
            cell.imgview.image = [UIImage imageWithData:data];
            cell.singName.text = _singer.mvtitle;
            cell.manName.text = _singer.singer_name;
            cell.mytitle.text = _singer.mvdesc;
        }
        return cell;
    } else {
        myCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"2" forIndexPath:indexPath];
        cell.frame = CGRectMake(0, 200, self.view.bounds.size.width / 2, 100);
        if (_arr.count != 0) {
            _listobjc = _arr[indexPath.row];
            _singer = [[Singer alloc]initWithPicurl:_singer.picurl mvtitle:_singer.mvtitle mvdesc:_singer.mvdesc singer_name:_singer.singer_name];
            _singer = _listobjc.mvlist[indexPath.row];
            NSURL *url = [NSURL URLWithString:_singer.picurl];
            NSData *data = [NSData dataWithContentsOfURL:url];
            cell.imgview.image = [UIImage imageWithData:data];
            cell.blackName.text = _singer.mvtitle;
            cell.blackTitle.text = _singer.singer_name;
            cell.grayName.text = _singer.mvdesc;

        }
            return cell;
    }
}
- (void)getup {
    self.arr = [NSMutableArray array];
    self.Arr = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:10];
    request.HTTPMethod = @"GET";
    //通信管道发送请求给服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        NSMutableString *str = [[NSMutableString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//        [str deleteCharactersInRange:NSMakeRange(0, 6)];
//        NSData *JSONdata = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *array = Dic[@"data"][@"contents"][@"list"];
        for (NSDictionary *dic in array) {
            list *listobjc = [list contentTitleWithDictionary:dic];
            [_arr addObject:listobjc];
        }
        NSArray *Array = Dic[@"data"][@"mv_show_play"];
        for (NSDictionary *Dic in Array) {
            Play *play = [Play contentTitleWithDictionary:Dic];
            [_Arr addObject:play];
        }
        [_collectionView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
